package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Maquina` (
  `idMaquina` INT NULL AUTO_INCREMENT ,
  `Laboratorio_idLaboratorio` INT NOT NULL ,
  `nome` VARCHAR(100) NULL ,
  `descricao` VARCHAR(100) NULL ,
  `patrimonio` VARCHAR(45) NULL ,
  `dataCompra` DATETIME NULL ,
  `dataFimGarantia` DATETIME NULL ,
  `garantia` VARCHAR(45) NULL ,
  PRIMARY KEY (`idMaquina`) ,
  INDEX `fk_Maquina_Laboratorio1` (`Laboratorio_idLaboratorio` ASC) ,
  CONSTRAINT `fk_Maquina_Laboratorio1`
    FOREIGN KEY (`Laboratorio_idLaboratorio` )
    REFERENCES `mydb`.`Laboratorio` (`idLaboratorio` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;*/
public class Maquina {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Maquina.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idMaquina;
    private Laboratorio Laboratorio_idLaboratorio;
    private String nome;
    private String descricao;
    private String patrimonio;
    private java.util.Date dataCompra;
    private java.util.Date dataFimGarantia;
    private String garantia;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Maquina(int idMaquina) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idMaquina = '" + idMaquina + "'");
            rs.next();
            this.idMaquina = rs.getInt("idMaquina");
            this.Laboratorio_idLaboratorio = new Laboratorio(rs.getInt("Laboratorio_idLaboratorio"));
            this.nome = rs.getString("nome");
            this.descricao = rs.getString("descricao");
            this.patrimonio = rs.getString("patrimonio");
            this.dataCompra = rs.getDate("dataCompra");
            this.dataFimGarantia = rs.getDate("dataFimGarantia");
            this.garantia = rs.getString("garantia");
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Maquina:";
            s += "idMaquina=" + idMaquina;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idMaquina() {
        return this.idMaquina;
    }

    public Laboratorio Laboratorio_idLaboratorio() {
        return this.Laboratorio_idLaboratorio;
    }

    public String nome() {
        return this.nome;
    }

    public String descricao() {
        return this.descricao;
    }

    public String patrimonio() {
        return this.patrimonio;
    }

    public java.util.Date dataCompra() {
        return this.dataCompra;
    }

    public java.util.Date dataFimGarantia() {
        return this.dataFimGarantia;
    }

    public String garantia() {
        return this.garantia;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idMaquina(int idMaquina) {
        boolean ok = update("idMaquina", idMaquina);
        if (ok) {
            this.idMaquina = idMaquina;
        }
        return ok;
    }
    public boolean Laboratorio_idLaboratorio(Laboratorio Laboratorio_idLaboratorio) {
        boolean ok = update("Laboratorio_idLaboratorio", Laboratorio_idLaboratorio.idLaboratorio());
        if (ok) {
            this.Laboratorio_idLaboratorio = Laboratorio_idLaboratorio;
        }
        return ok;
    }
    public boolean nome(String nome) {
        boolean ok = update("nome", nome);
        if (ok) {
            this.nome = nome;
        }
        return ok;
    }
    public boolean descricao(String descricao) {
        boolean ok = update("descricao", descricao);
        if (ok) {
            this.descricao = descricao;
        }
        return ok;
    }
    public boolean patrimonio(String patrimonio) {
        boolean ok = update("patrimonio", patrimonio);
        if (ok) {
            this.patrimonio = patrimonio;
        }
        return ok;
    }
    public boolean dataCompra(java.util.Date dataCompra) {
        boolean ok = update("dataCompra", dataCompra);
        if (ok) {
            this.dataCompra = dataCompra;
        }
        return ok;
    }
    public boolean dataFimGarantia(java.util.Date dataFimGarantia) {
        boolean ok = update("dataFimGarantia", dataFimGarantia);
        if (ok) {
            this.dataFimGarantia = dataFimGarantia;
        }
        return ok;
    }
    public boolean garantia(String garantia) {
        boolean ok = update("garantia", garantia);
        if (ok) {
            this.garantia = garantia;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Maquina \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idMaquina = '" + idMaquina + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Maquina> searchIdMaquina(int valor) {
        return search("idMaquina", valor+"");
    }
    public static java.util.ArrayList<Maquina> searchLaboratorio_idLaboratorio(Laboratorio valor) {
        return search("Laboratorio_idLaboratorio", valor.idLaboratorio()+"");
    }
    public static java.util.ArrayList<Maquina> searchNome(String valor) {
        return search("nome", valor+"");
    }
    public static java.util.ArrayList<Maquina> searchDescricao(String valor) {
        return search("descricao", valor+"");
    }
    public static java.util.ArrayList<Maquina> searchPatrimonio(String valor) {
        return search("patrimonio", valor+"");
    }
    public static java.util.ArrayList<Maquina> searchDataCompra(java.util.Date valor) {
        return search("dataCompra", valor+"");
    }
    public static java.util.ArrayList<Maquina> searchDataFimGarantia(java.util.Date valor) {
        return search("dataFimGarantia", valor+"");
    }
    public static java.util.ArrayList<Maquina> searchGarantia(String valor) {
        return search("garantia", valor+"");
    }

    public static java.util.ArrayList<Maquina> search(String campo, String valor) {
        try {
            java.util.ArrayList<Maquina> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idMaquina FROM Maquina "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Maquina(rs.getInt("idMaquina")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Maquina newMaquina(int idMaquina, Laboratorio Laboratorio_idLaboratorio, String nome, String descricao, String patrimonio, java.util.Date dataCompra, java.util.Date dataFimGarantia, String garantia){
            String sql = "INSERT INTO Maquina (idMaquina, Laboratorio_idLaboratorio, nome, descricao, patrimonio, dataCompra, dataFimGarantia, garantia)"
                    + " VALUES "
                    + "('"+idMaquina+"', '"+Laboratorio_idLaboratorio.idLaboratorio() + "', '"+nome+"', '"+descricao+"', '"+patrimonio+"', '"+(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format((java.util.Date) dataCompra)+"', '"+(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format((java.util.Date) dataFimGarantia)+"', '"+garantia+"');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Maquina(rs.getInt("idMaquina"));
            } else {
                return new Maquina(idMaquina);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}